﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Twipler.App;
using Twipler.Xtensions;
using Twipler.WebApp;

namespace Settings.Controls
{
    public partial class ListPaging : System.Web.UI.UserControl
    {
        public int StartIndex { get; set; }

        public delegate void OnPageSelectionChange();
        public event OnPageSelectionChange PageSelectionChange;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                drpPerPage.Items.Clear();
                drpPerPage.Items.AddRange(
                    new ListItem[] { 
                        new ListItem("10", "10"),
                        new ListItem("25", "25"), 
                        new ListItem("50", "50"), 
                        new ListItem("100", "100"),
                        new ListItem("500", "500")
                    });

                drpPerPage.SelectedValue = Current.ResultsPerPage.ToString();
            }
        }

        public void UpdateDisplay(string linkPage, int dataLength)
        {
            int page = Request.QueryStringInt("Page").GetValueOrDefault(0);
            int len = dataLength / Current.ResultsPerPage;

            if ((dataLength % Current.ResultsPerPage) == 0)
                len--;

            StringBuilder html = new StringBuilder();

            if (len == 0)
            {
                html.AppendFormat("<b>1</b>");
                StartIndex = 0;
            }
            else
            {
                if (page < 0)
                    page = 0;

                if (page > len)
                    page = len;

                if (dataLength < Current.ResultsPerPage)
                {
                    StartIndex = 0;
                    return;
                }

                for (int i = 0; i <= len; )
                {
                    if (i == page)
                        html.AppendFormat("<b>{0}</b>", ++i);
                    else
                        html.AppendFormat("<a href=\"{0}\">{1}</a>",
                            QueryStringHash.Protect(linkPage + "Page=" + i),
                            ++i);
                }

                StartIndex = page * Current.ResultsPerPage;            
            }

            litPaging.Text = html.ToString();
            
        }

        protected void drpPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartIndex = 0;
            Current.ResultsPerPage = int.Parse(drpPerPage.SelectedValue);
            if (PageSelectionChange != null)
                PageSelectionChange();
        }

    }
}